// src/stores/commentStore.js
import { defineStore } from 'pinia'
import { ref } from 'vue'
import axios from 'axios'

export const useCommentStore = defineStore('comment', () => {
  const comments = ref([])
  const loading = ref(false)
  const error = ref(null)

  const addComment = async (commentData) => {
    try {
      loading.value = true
      const response = await axios.post('/api/comment/addComment', commentData)
      if (response.data.status === 0) {
        return true
      }
    } catch (err) {
      error.value = err.response?.data?.message || '发布评论失败'
      return false
    } finally {
      loading.value = false
    }
  }

  const deleteComment = async (id) => {
    try {
      loading.value = true
      const response = await axios.post('/api/comment/delComment', { id })
      if (response.data.status === 0) {
        return true
      }
    } catch (err) {
      error.value = err.response?.data?.message || '删除评论失败'
      return false
    } finally {
      loading.value = false
    }
  }

  return {
    comments,
    loading,
    error,
    addComment,
    deleteComment
  }
})